<?
include_once($_SERVER['DOCUMENT_ROOT']."/lucy/config.php");
include_once(INC.'sess.php');
include_once(CLA."speech.class.php");
include_once(CLA."lucy_feelings.class.php");

if($_POST['word'] != "" AND $_POST['feeling'] != "" AND $_POST['type'] != ""){
	$word = str_replace(" ", "+", trim($_POST['word']));
	$type = str_replace(" ", "+", trim($_POST['type']));
	$feeling = $_POST['feeling'];

	
	$speech = new Speech($feeling, $word, $type);
	if($speech->save()){
		$_SESSION['print']['echo'] = "Pievienots jauns izteiciens <u><b>$word</b></u>";
		$mod_name = "print";
		$send = "<meta http-equiv='refresh' content=\"0;url=http://localhost/lucy/sys/mod/".$mod_name.".php?inSess=".session_id()."\">";
		echo $send;
	}
}elseif($_POST['cancel'] == 1){
		$_SESSION['print']['echo'] = "Neko nesaglabāju :(";
		
		$lucy_feeling = new Lucy_feelings();
		$lucy_feeling->change(1);
		
		$mod_name = "print";
		$send = "<meta http-equiv='refresh' content=\"0;url=http://localhost/lucy/sys/mod/".$mod_name.".php?inSess=".session_id()."\">";
		echo $send;
}

if(is_word($_SESSION['feeling'])){
	//	USER FEELING
	$user_speech = new Speech();

	$user_speech->feeling = $user_speech->get_feeling($_SESSION['feeling']);
	$user_speech->type = $user_speech->get_type($_SESSION['feeling']);

	//	LUCY RESPONSE
	$lucy_speech = new Speech();

	$lucy_speech->set_feeling($user_speech->feeling); /*		----	*/

	$type = $user_speech->get_response_type();
	$lucy_speech->set_type($type);

	$return = $lucy_speech->get_word();
	$return = (rand(0, 2) == 0) ? 0 : $return;
	
	if($user_speech->feeling == $lucy_speech->feeling){
		$me_to = "es arī";
	}else{
		$me_to = (rand(0, 1) == 0) ? 0 : "es gan";
	}
	
	if($return){
		$_SESSION['print']['echo'] = $me_to." ".$return;
		$mod_name = "print";
		$send = "<meta http-equiv='refresh' content=\"0;url=http://localhost/lucy/sys/mod/".$mod_name.".php?inSess=".session_id()."\">";
		echo $send;
	}else{
		$_SESSION['help']['type'] = $lucy_speech->type;
		$_SESSION['help']['feeling'] = $lucy_speech->feeling;
		$_SESSION['help']['response_to'] = $_SESSION['feeling'];
		
		$mod_name = "help_response";
		$send = "<meta http-equiv='refresh' content=\"0;url=http://localhost/lucy/sys/mod/".$mod_name.".php?inSess=".session_id()."\">";
		echo $send;
	}
	
}else{


	echo "Šī sajūta man nav zināma. :( <br/>";
	echo "Kāds ir šīs sajūtas novērtējums?";
	?>

	<form action="" method="POST">
		<table border="0">
			<tr>
				<td>
					sajūta:
				</td>
				<td>
					<input type="text" name="word" value="<?=$_SESSION['feeling'];?>">
				</td>
			</tr>	
			<tr>
				<td>
					tips:
				</td>
				<td>
					<input type="text" name="type" value="">
				</td>
			</tr>		
			<tr>
				<td>
					feeling:
				</td>
				<td>
					<select name="feeling">
						<? for ($i=1;$i<=5;$i++){ ?>
							<option value="<?=$i;?>"><?=$i;?></option>
						<? } ?>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					
				</td>
				<td>
					<input type="submit" name="submit" value="Saglabāt">
				</td>
			</tr>
		</table>
	</form>

	<form action="" method="POST">
		<input type="hidden" name="cancel" value="1">
		<input type="submit" name="submit" value="Atcelt">
	</form>
<?
}
?>